﻿#if UNITY_IOS
using System.Collections.Generic;
using UnityEngine;

namespace Notifications.Runtime.iOS
{
    public class iOSBackgroundNotificationManager : IGameBackgroundNotificationsPlatform
    {
        private Dictionary<int, iOSBackgroundGameNotification> _backgroundNotifications = new Dictionary<int, iOSBackgroundGameNotification>();
        private bool _isForeground = true;

        public void Init()
        {
            RequestAuthorization();
        }

        private void RequestAuthorization()
        {
            iOSBackgroundNotificationCenter.RequestAuthorization();
        }
        
        public void ScheduleBackgroundNotification(IGameBackgroundNotification gameNotification)
        {
            iOSBackgroundGameNotification notification = gameNotification as iOSBackgroundGameNotification;
            if (notification!= null)
            {
                _backgroundNotifications[notification.Id] = notification;
                if (!_isForeground)
                {
                    iOSBackgroundNotificationCenter.SendNotification(notification);
                }
            }
        }
        
        public void UpdateDateBackgroundNotification(IGameBackgroundNotification gameNotification)
        {
            if (_backgroundNotifications.ContainsKey(gameNotification.GetId()))
            {
                iOSBackgroundGameNotification notification = gameNotification as iOSBackgroundGameNotification;
                if (notification != null)
                {
                    if (!_isForeground)
                    {
                        iOSBackgroundNotificationCenter.SendNotification(notification);
                    }
                    else
                    {
                        iOSBackgroundNotificationCenter.RemoveDeliveredNotification(gameNotification.GetId());
                    }
                }
            }
        }

        public void CancelBackgroundNotification(int id)
        {
            if (_backgroundNotifications.ContainsKey(id))
            {
                iOSBackgroundNotificationCenter.RemoveDeliveredNotification(id);
                _backgroundNotifications.Remove(id);
            }
        }
        public void CancelAllBackgroundNotifications()
        {
            foreach (var notification in _backgroundNotifications.Values)
            {
                iOSBackgroundNotificationCenter.RemoveDeliveredNotification(notification.GetId());
            }
            _backgroundNotifications.Clear();
        }

        public void OnForeground()
        {
            _isForeground = true;
            foreach (var notification in _backgroundNotifications.Values)
            {
                iOSBackgroundNotificationCenter.RemoveDeliveredNotification(notification.GetId());
            }
        }

        public void OnBackground()
        {
            _isForeground = false;
            foreach (var notification in _backgroundNotifications.Values)
            {
                if (notification.NeedClose)
                {
                    iOSBackgroundNotificationCenter.RemoveDeliveredNotification(notification.GetId());
                }
                else
                {
                    iOSBackgroundNotificationCenter.SendNotification(notification);
                }
            }
        }

        public IGameBackgroundNotification CreateBackgroundNotification()
        {
            return new iOSBackgroundGameNotification();
        }

        public void Dispose()
        {
            CancelAllBackgroundNotifications();
        }
        
    }
}
#endif